<?php
    session_start();
    ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <style type='text/css'>
            <!--
            body {
                background-color: #666666;
                margin-left: 17px;
                margin-top: 0px;
                margin-right: 0px;
                margin-bottom: 0px;
            }
            -->
        </style>
        <link rel='stylesheet' href='triumph.css' TYPE='text/css' MEDIA='screen'>
    </head>
    
<?php
    include "Employee.php";
    include "EmployeeDao.php";
    include "Ticket.php";
    include "TicketDao.php";
    include "Customer.php";
    include "CustomerDao.php";
    include "CustContactLog.php";
    include "CustContactLogDao.php";
    include "WorkDone.php";
    include "WorkDoneDao.php";
    include "Datasource.php";
    include "ProductDetails.php";
    include "ProductDetailsDao.php";
    include "CustomerProductDao.php";
    include "CustomerProduct.php";

    $conn = new Datasource("localhost","theanykey","root","");

    extract($_POST);
    $ticket = new Ticket();
    $ticketDao = new TicketDao();
    $ticket->setTicketID($ticketidNo);
    $ticket->setCurrentEmpAssigned($empid);
    $ticket->setCustomerID($id);
    if(substr($cmplt, 2, 1) == "-" && strlen($cmplt) == 10)
    {
        $month = substr($cmplt, 0, 2);
        $day = substr($cmplt, 3, 2);
        $year = substr($cmplt, strlen($cmplt) - 4);
        if(checkdate($month, $day, $year))
        {
             $ticket->setExpectCmpltDate("$year-$month-$day");
        }
        else
        {
            $ticket->setExpectCmpltDate("");
        }
    }
    else if(substr($cmplt, 4, 1) == "-" && strlen($cmplt) == 10)
    {
        $year = substr($cmplt, 0, 4);
        $month = substr($cmplt, 5, 2);
        $day = substr($cmplt, strlen($cmplt) - 2);
        if(checkdate($month, $day, $year))
        {
             $ticket->setExpectCmpltDate("$year-$month-$day");
        }
        else
        {
            $ticket->setExpectCmpltDate("");
        }
    }
    else
    {
        $ticket->setExpectCmpltDate("");
    }
    if($ticketDao->save($conn, $ticket))
    {
        //the count logic fails when a ticket is deleted because the next
        //key in sequence may not always be the number of rows, in this case we
        //use the max(ticketID) created newly in the dao
        $ticketID = $ticketidNo;

        print("<h1>Ticket # $ticketID Successfully Updated</h1>");
        //adding workdone entries
        if(!(($notes == "") && ($dateDone == "") && ($hours == "")))
        {
            $workDone = new WorkDone();
            $workDoneDao = new WorkDoneDao();

            $workDone->setDateDone($dateDone);
            $workDone->setTechAssigned($techID);
            $workDone->setHrsWorkDone($hours);
            $workDone->setNotes(mysql_escape_string($notes));
            $workDone->setTicketID($ticketID);

           if(!$workDoneDao->create($conn, $workDone))
           {
               ?>
                    <br/>
                    <h2 style="color: Red">Work Done for Ticket Add Failed</h2>
                <?php
           }
        }//end notes check if

        if(true)
        {
            if($_POST['hdnID'])
           {


                //$conn = new Datasource("localhost","theanykey","root","");
                $tempCustomerProduct = new CustomerProduct();
                $custProduct = new CustomerProductDao();
                $tempCustomerProduct->setProductDetailsID($_POST['hdnID']);
                $tempCustomerProduct->setTicketID($ticketID);


                $ticketID = $tempCustomerProduct->getCustomerProductID();
                $productDetID = $tempCustomerProduct->getProductDetailsID();
                echo $ticketID . " " . $productDetID;
                if(!$custProduct->create($conn, $tempCustomerProduct))
                {
                    echo("The entry failed being added to the data base.");
                }

            }
        }

        if(($custContactDate) && ($txtAreaCustContactNotes))
        {
            $objCustContact = new CustContactLog();
            $objCustContDAO = new CustContactLogDao();
            $objCustContact->setContactNotes($txtAreaCustContactNotes);
            $objCustContact->setCustID($id);
            $objCustContact->setDateContacted($custContactDate);
            $objCustContact->setEmpID($empid);
            $objCustContact->setTicketID($ticketidNo);
            if(!$objCustContDAO->create($conn, $objCustContact))
            {
               ?>
                    <br/>
                    <h2 style="color: Red">Customer Contact Log Entry for Ticket Add Failed</h2>
                <?php
            }
        }

     }//end ticket add if
     else
     {
            ?>
                    <h1 style="color: Red">Work Done for Ticket Add Failed</h1>
            <?php
     }
?>

    <body onload='window.location.href="TicketUpdate.php?ticketID=<?php print($ticketidNo);?>&prevUpdate=success";'>
    
    </body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

</html>